⛓️ devops | September 16, 2021
알고가기!
해야할 task를 정의하는 action
과 조건문인 when
를 이용해 동적으로 플레이북을 작성할 수 있습니다.
setup
모듈을 이용해 인벤토리 서버에 대한 정보 가져오기$ ansible all -m setup >> facts.txt
플레이북에 사용할 yaml 파일 작성
$ vi test.yml
# [test.yml]
---
- name: Install nginx on the nodes
hosts: all
become: yes
tasks:
- name: install epel-release for CentOS
action: "{{ ansible_pkg_mgr }} name=epel-release state=latest"
when: ansible_distribution == 'CentOS'
- name: install nginx web server for CentOS
action: "{{ ansible_pkg_mgr }} name=nginx state=present"
when: ansible_distribution == 'CentOS'
- name: upload default index.html for web server
get_url: url=https://www.nginx.com dest=/usr/share/nginx/html/ mode=0644
when: ansible_distribution == 'CentOS'
- name: start nginx web server
service: name=nginx state=started
when: ansible_distribution == 'CentOS'
- name: install nginx web server for Ubuntu
action: "{{ ansible_pkg_mgr }} name=nginx state=present update_cache=yes"
when: ansible_distribution == 'Ubuntu'
- name: upload default index.html for web server
get_url: url=https://www.nginx.com dest=/usr/share/nginx/html/
mode=0644 validate_certs=no
when: ansible_distribution == 'Ubuntu'
$ ansible-playbook test.yml
검증
gather_facts
의 기본 값은 yes이며, 위에서 정보를 표시해줍니다. 빠른 성능과 정보가 필요 없다면 no로 설정하는 것이 좋습니다.when
조건에 맞지 않는 것은 skipping 된 것을 확인할 수 있습니다. 아래 예제에선 centos 조건문을 걸었는데 스킵 된 인벤토리 서버는 ubuntu임$ vi test_remove.yml
# [test_remove.yml]
---
- name: Remove nginx on the nodes
hosts: all
become: yes
tasks:
- name: remove epel-release for CentOS
action: "{{ ansible_pkg_mgr }} name=epel-release state=absent"
when: ansible_distribution == 'CentOS'
- name: remove nginx web server for CentOS
action: "{{ ansible_pkg_mgr }} name=nginx state=absent"
when: ansible_distribution == 'CentOS'
- name: remove nginx web server
action: "{{ ansible_pkg_mgr }} name=nginx state=absent autoremove=yes"
when: ansible_distribution == 'Ubuntu'
...
$ ansible-playbook test_remove.yml
플레이북에 사용할 yaml 파일 작성
mode: 0777
옵션 : chmod 777과 동일/etc/exports
옵션 : 접속이 허용된 대역을 추가하기 위한 설정$ vi nfs.yml
# [nfs.yml]
- name: Setup for nfs server
hosts: localhost # ansible 서버인 localhost에 설치
gather_facts: no
tasks:
- name: make nfs_shared directory
file: # file 모듈
path: /root/nfs_shared
state: directory
mode: 0777 # chmod 777과 동일
- name: configure /etc/exports # 접속이 허용된 대역을 추가하기 위한 설정
lineinfile:
path: /etc/exports
line: /root/nfs_shared 192.168.56.0/24(rw,sync)
- name: install NFS
yum:
name: nfs-utils # nfs 서버 설치
state: present
- name: nfs service start
service:
name: nfs-server
state: restarted
enabled: yes
- name: Setup for nfs clients
hosts: centos
gather_facts: no
tasks:
- name: make nfs_client directory
file:
path: /root/nfs
state: directory
- name: install NFS
yum:
name: nfs-utils
state: present
- name: mount point directory as client
mount:
path: /root/nfs
src: 192.168.56.105:/root/nfs_shared # ansible의 ip를 할당
fstype: nfs
state: mounted
- name: Setup for nfs clients Ubuntu
hosts: ubuntu
gather_facts: no
tasks:
- name: make nfs_client directory
file:
path: /root/nfs
state: directory
- name: Install NFS-Utils
apt:
pkg: nfs-common
state: present
update_cache: yes # apt update -y
- name: mount point directory as client
mount:
path: /root/nfs
src: 192.168.56.105:/root/nfs_shared
fstype: nfs
opts: nfsvers=3
state: mounted
$ ansible-playbook nfs.yml